package com.winbomb.kingcard.screens;

import android.graphics.Color;
import android.graphics.Paint;

import com.winbomb.kingcard.Assets;
import com.winbomb.kingcard.KingCard;
import com.winbomb.kingcard.base.GameLogic;
import com.winbomb.kingcard.framework.Game;
import com.winbomb.kingcard.framework.Screen;
import com.winbomb.kingcard.framework.Graphics.PixmapFormat;
import com.winbomb.kingcard.framework.Input.TouchEvent;
import com.winbomb.kingcard.framework.impl.TouchArea;

public class MainScreen extends Screen {

	private static final TouchArea btnNewGame = new TouchArea(42, 111, 121, 30);
	private static final TouchArea btnTutor = new TouchArea(42, 155, 121, 30);

	// 新游戏的按钮
	private static final TouchArea btnPlayerUp = new TouchArea(374, 94, 57, 49);
	private static final TouchArea btnPlayerDown = new TouchArea(256, 94, 57, 49);
	private static final TouchArea btnStartGame = new TouchArea(310, 164, 75, 31);

	// 帮助的按钮
	private static final TouchArea btnGameTutor = new TouchArea(254, 60, 186, 42);
	private static final TouchArea btnBattleTutor = new TouchArea(254, 106, 186, 42);
	private static final TouchArea btnScoreTutor = new TouchArea(254, 154, 75, 31);

	private static final TouchArea btnEnableMusic = new TouchArea(50, 272, 35, 35);
	private static final TouchArea btnEnableSound = new TouchArea(88, 272, 35, 35);
	private static final TouchArea btnInfo = new TouchArea(125, 272, 35, 35);

	private static final Paint PAINT = new Paint();
	private State state;
	private int playerCnt = 8;

	private int bgY = -216;

	public MainScreen(Game game) {
		super(game);
		this.state = State.NEW_START;

		PAINT.setColor(Color.WHITE);
		PAINT.setTextSize(32);
	}

	@Override
	public void dispose() {
		Assets.bg_music.stop();
	}

	@Override
	public void pause() {
		Assets.bg_music.pause();
	}

	@Override
	public void present(float deltaTime) {
		g.drawPixmap(Assets.main_bg, 0, 0);

		// 绘制音乐
		g.drawPixmap(Assets.music_setting, 50, 272, 0, (KingCard.isMusicOn) ? 0 : 35, 35, 35);
		g.drawPixmap(Assets.music_setting, 88, 272, 35, (KingCard.isSoundOn) ? 0 : 35, 35, 35);
		g.drawPixmap(Assets.music_setting, 125, 272, 70, 0, 35, 35);

		if (state == State.TO_NEW_GAME) {
			g.drawPixmap(Assets.new_game, 0, bgY);
			bgY += 5;
			if (bgY >= 0) {
				state = State.NEW_GAME;
				bgY = -216;
			}
		} else if (state == State.TO_TUTORIAL) {
			g.drawPixmap(Assets.game_tutor, 0, bgY);
			bgY += 4;
			if (bgY >= 0) {
				state = State.TUTORIAL;
				bgY = -216;
			}

		} else if (state == State.NEW_GAME) {
			g.drawPixmap(Assets.new_game, 0, 0);
			String text = String.valueOf(playerCnt);
			int x = 322 + (int) ((44 - PAINT.measureText(text)) / 2);
			g.drawText(playerCnt, x, 128, PAINT);

			if (playerCnt <= 2) {
				g.drawPixmap(Assets.main_elems, 256, 94, 0, 0, 60, 48);
			} else if (playerCnt >= 12) {
				g.drawPixmap(Assets.main_elems, 371, 93, 60, 0, 60, 48);
			}
		} else if (state == State.TUTORIAL) {
			g.drawPixmap(Assets.game_tutor, 0, 0);
		}
	}

	@Override
	public void resume() {
		if (KingCard.isMusicOn) {
			Assets.bg_music.dispose();
			Assets.bg_music = audio.newMusic("music/bg_music.ogg");
			Assets.bg_music.setLooping(true);
			Assets.bg_music.setVolume(0.5f);
			Assets.bg_music.play();
		}
	}

	@Override
	public void update(float deltaTime) {

		updateTouchEvents();

		for (TouchEvent event : touchEvents) {
			if (event.type != TouchEvent.TOUCH_UP) {
				continue;
			}

			if (state != State.TO_NEW_GAME && state != State.TO_TUTORIAL) {
				if (btnNewGame.isBound(event)) {
					state = State.TO_NEW_GAME;
				} else if (btnTutor.isBound(event)) {
					state = State.TO_TUTORIAL;
				}

				if (btnEnableMusic.isBound(event)) {
					KingCard.isMusicOn = !KingCard.isMusicOn;
					if (KingCard.isMusicOn) {
						Assets.bg_music.play();
					} else {
						Assets.bg_music.stop();
					}

				} else if (btnEnableSound.isBound(event)) {
					KingCard.isSoundOn = !KingCard.isSoundOn;
				} else if (btnInfo.isBound(event)) {

				}

			}

			if (state == State.NEW_GAME) {
				if (btnPlayerUp.isBound(event)) {
					if (playerCnt < 12) {
						playerCnt++;
					}
				}

				else if (btnPlayerDown.isBound(event)) {
					if (playerCnt > 2) {
						playerCnt--;
					}
				}

				else if (btnStartGame.isBound(event)) {
					GameLogic gameImpl = new GameLogic(game, playerCnt);
					gameImpl.start();
					game.setScreen(new GameScreen(game, gameImpl));
				}

			} else if (state == State.TUTORIAL) {

				// 游戏规则指导
				if (btnGameTutor.isBound(event)) {
					Assets.h1_bg1 = g.newPixmap("help1/h1_bg1.png", PixmapFormat.ARGB4444);
					Assets.h1_bg2 = g.newPixmap("help1/h1_bg2.png", PixmapFormat.ARGB4444);
					Assets.h1_bg3 = g.newPixmap("help1/h1_bg3.png", PixmapFormat.ARGB4444);
					Assets.h1_s1 = g.newPixmap("help1/h1_s1.png", PixmapFormat.ARGB4444);
					Assets.h1_s2 = g.newPixmap("help1/h1_s2.png", PixmapFormat.ARGB4444);
					Assets.h1_s3 = g.newPixmap("help1/h1_s3.png", PixmapFormat.ARGB4444);
					Assets.h1_s4 = g.newPixmap("help1/h1_s4.png", PixmapFormat.ARGB4444);
					Assets.h1_s5 = g.newPixmap("help1/h1_s5.png", PixmapFormat.ARGB4444);
					Assets.h1_s6 = g.newPixmap("help1/h1_s6.png", PixmapFormat.ARGB4444);
					Assets.h1_s7 = g.newPixmap("help1/h1_s7.png", PixmapFormat.ARGB4444);
					Assets.h1_s8 = g.newPixmap("help1/h1_s8.png", PixmapFormat.ARGB4444);
					Assets.h1_s9 = g.newPixmap("help1/h1_s9.png", PixmapFormat.ARGB4444);
					Assets.h1_s10 = g.newPixmap("help1/h1_s10.png", PixmapFormat.ARGB4444);
					Assets.h1_s11 = g.newPixmap("help1/h1_s11.png", PixmapFormat.ARGB4444);
					Assets.h1_s12 = g.newPixmap("help1/h1_s12.png", PixmapFormat.ARGB4444);
					Assets.h1_s13 = g.newPixmap("help1/h1_s13.png", PixmapFormat.ARGB4444);

					game.setScreen(new GameHelpScreen(game));
				}

				// 战斗规则指导
				if (btnBattleTutor.isBound(event)) {
					Assets.h2_bg1 = g.newPixmap("help2/h2_bg1.png", PixmapFormat.ARGB4444);
					Assets.h2_bg2 = g.newPixmap("help2/h2_bg2.png", PixmapFormat.ARGB4444);
					Assets.h2_s1 = g.newPixmap("help2/h2_s1.png", PixmapFormat.ARGB4444);
					Assets.h2_s2 = g.newPixmap("help2/h2_s2.png", PixmapFormat.ARGB4444);
					Assets.h2_s3 = g.newPixmap("help2/h2_s3.png", PixmapFormat.ARGB4444);
					Assets.h2_s4 = g.newPixmap("help2/h2_s4.png", PixmapFormat.ARGB4444);
					Assets.h2_s5 = g.newPixmap("help2/h2_s5.png", PixmapFormat.ARGB4444);
					Assets.h2_s6 = g.newPixmap("help2/h2_s6.png", PixmapFormat.ARGB4444);

					game.setScreen(new BattleHelpScreen(game));
				}

				if (btnScoreTutor.isBound(event)) {

				}

			}
		}
	}

	private static enum State {
		NEW_START, TO_NEW_GAME, NEW_GAME, TO_TUTORIAL, TUTORIAL
	}

}
